//
//  MonitorViewController.m
//  Light
//
//  Created by 骁唐 on 15/12/2.
//  Copyright © 2015年 骁唐. All rights reserved.
//

#import "MonitorViewController.h"
#import "WaveView.h"
#import "HLSonogramView.h"
#import "NSData+Hex.h"
#import "AppDelegate.h"
#import "constantDefinition.h"

@interface MonitorViewController ()
@property (weak, nonatomic) IBOutlet UIButton *headBtn;
@property (weak, nonatomic) IBOutlet UIButton *heartBtn;

@property (weak, nonatomic) IBOutlet UIView *waveformView;
@property (strong,nonatomic) WaveView* waveView;

@property (strong,nonatomic) NSMutableArray *converAry;

@property (strong,nonatomic) UIWindow *window;

@property (assign,nonatomic) BOOL removeAry;

@property (strong,nonatomic) UIView *parentView;

@end

@implementation MonitorViewController
{
    NSTimer *addDataTimer;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //强制横屏
    [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIInterfaceOrientationLandscapeRight] forKey:@"orientation"];
    
    self.headBtn.selected = YES;
    self.heartBtn.selected = NO;
   
//    self.waveView = [[WaveView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH-SCREEN_HEIGHT/2-5, SCREEN_HEIGHT)];
    self.waveView = [WaveView sharingWaveViewWithFrame:CGRectMake(0, 0, SCREEN_WIDTH-SCREEN_HEIGHT/2-5, SCREEN_HEIGHT)];
    self.waveView.backgroundColor = [UIColor clearColor];
    UIView *backView = [[UIView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/25,SCREEN_HEIGHT/15, 60, 30)];
    UIImageView *backImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 5, 20, 20)];
    backImage.image = [UIImage imageNamed:@"left"];
    
    
    UILabel *backlLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, 40, 30)];
    backlLabel.text = @"返回";
    backlLabel.textColor = [UIColor whiteColor];
    
    UIButton *backBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 60, 30)];

    [backBtn addTarget:self action:@selector(backClickB) forControlEvents:UIControlEventTouchUpInside];
    UITapGestureRecognizer *click = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(backClickB)];
    [backView addGestureRecognizer:click];
    [backView addSubview:backImage];
    [backView addSubview:backlLabel];
    [backView addSubview:backBtn];

    backView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.waveView];
    
    self.converAry = [[NSMutableArray alloc] init];

    addDataTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(redMonitor) userInfo:nil repeats:YES];
    [self.waveView addSubview:backView];
    
//    _parentView = [[UIView alloc] init];
//    NSArray* windows = [UIApplication sharedApplication].windows;
//    _window = [windows objectAtIndex:0];
//    //keep the first subview
//    if(_window.subviews.count > 0){
//        _parentView = [_window.subviews objectAtIndex:0];
//    }
//    
//    [_parentView addSubview:backView];
//    NSArray *datas = [self datasFromFiles];
//    NSArray *convData = [self convertDatas:datas];
//    [self updateOutterData:convData];
  
}



-(void)timerRemove
{
//    [NSTimer scheduledTimerWithTimeInterval:20 target:self selector:@selector(removeMonitorAry) userInfo:nil repeats:YES];
    self.removeAry = YES;
}

#pragma mark -
#pragma mark 移除数据
-(void)redMonitor
{
    AppDelegate *delegate = [[UIApplication sharedApplication] delegate];
//    NSArray *convData = [self convertDatas:delegate.MonitorAry];
    [self.view addSubview:self.waveView];
//    [self.waveView updateWaveWithData:convData];
    [self.converAry removeAllObjects];
    if (delegate.MonitorAry.count > 1250) {
        [delegate.MonitorAry removeObjectsInRange:NSMakeRange(0, 1250)];
    }
    
//    [delegate.MonitorAry removeObjectsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0,delegate.MonitorAry.count -1)]];
    
//    AppDelegate *delegate = [[UIApplication sharedApplication]delegate];
//    NSArray *convData = [self convertDatas:delegate.MonitorAry];
//    [self.waveView up:convData];
//    convData = nil;
//    [self.converAry removeAllObjects];
}
//-(void)removeMonitorAry
//{
//     AppDelegate *delegate = [[UIApplication sharedApplication]delegate];
//    
//}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)headClick {
    self.headBtn.selected = YES;
    self.heartBtn.selected = NO;
    AppDelegate *delegate = [[UIApplication sharedApplication] delegate];
    delegate.currentColor = [UIColor blueColor];
    delegate.scope = 10000;

}

- (IBAction)heartClick {
    self.heartBtn.selected = YES;
    self.headBtn.selected = NO;
    AppDelegate *delegate = [[UIApplication sharedApplication] delegate];
    delegate.currentColor = [UIColor redColor];
    delegate.scope = 2000;
}

- (NSArray *)datasFromFiles{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"d" ofType:@""];
    
    NSString *data = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:NULL];
    data = [data stringByReplacingOccurrencesOfString:@"\n" withString:@""];
    data = [data stringByReplacingOccurrencesOfString:@" " withString:@""];
    data = [data stringByReplacingOccurrencesOfString:@"<" withString:@""];
    data = [data stringByReplacingOccurrencesOfString:@">" withString:@""];
    NSArray *arr = [data componentsSeparatedByString:@","];
    NSMutableArray *convertedData = [NSMutableArray array];
    for (NSString *d1 in arr) {
        NSData *demoData = [NSData hexStrToNSData:d1];
        [convertedData addObject:demoData];
    }
    return [NSArray arrayWithArray:convertedData];
}

#pragma mark -
#pragma mark 添加数据
- (NSArray *)convertDatas:(NSArray *)datas{
//    NSMutableArray *arr = [NSMutableArray array];
    for (NSData *data in datas) {
        uint8_t *b = (uint8_t *)data.bytes;
        long long t1 = b[1]*65536+b[2]*256+b[3];
        if (t1>8388607) {
            t1 = -1*(16777216-t1);
        }
        long long t2 = b[4]*65536+b[5]*256+b[6];
        if (t2>8388607) {
            t2 = -1*(16777216-t2);
        }
        long long t3 = b[7]*65536+b[8]*256+b[9];
        if (t3>8388607) {
            t3 = -1*(16777216-t3);
        }
        long long t4 = b[11]*65536+b[12]*256+b[13];
        if (t4>8388607) {
            t4 = -1*(16777216-t4);
        }
        
        long long t5 = b[14]*65536+b[15]*256+b[16];
        if (t5>8388607) {
            t5 = -1*(16777216-t5);
        }
        
        long long t6 = b[17]*65536+b[18]*256+b[19];
        if (t6>8388607) {
            t6 = -1*(16777216-t6);
        }
        [self.converAry addObject:@(t1)];
        [self.converAry addObject:@(t2)];
        [self.converAry addObject:@(t3)];
        [self.converAry addObject:@(t4)];
        [self.converAry addObject:@(t5)];
        [self.converAry addObject:@(t6)];
    }
    return [NSArray arrayWithArray:self.converAry];
}

-(void)viewDidDisappear:(BOOL)animated
{
    //强制竖屏
    [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:
                                        UIInterfaceOrientationPortrait]  forKey:@"orientation"];
}

-(void)dealloc
{
    [addDataTimer invalidate]; //释放
    addDataTimer = nil;
}

- (void)viewWillAppear:(BOOL)animated {
    //强制横屏
    [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIInterfaceOrientationLandscapeRight] forKey:@"orientation"];
}

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscape;
}
#pragma mark -
#pragma mark 返回上级界面 断开心电数据接收
-(void) backClickB
{
    AppDelegate *delegate = [[UIApplication sharedApplication]delegate];
    
    if (delegate.peripheral != nil) {
        Byte ACkValue[4] = {0};
        ACkValue[0] = 0xAE; ACkValue[1] = 0x01; ACkValue[2] = 0x06; ACkValue[3] = 0xAA;
        NSData *data = [NSData dataWithBytes:&ACkValue length:sizeof(ACkValue)];
        if ([delegate.writeCharacteristic.UUID.UUIDString isEqual:@"FFF2"]) {
            [delegate.peripheral writeValue:data
                          forCharacteristic:delegate.writeCharacteristic
                                       type:CBCharacteristicWriteWithResponse];
            
        }
    }
    [delegate.MonitorAry removeAllObjects];
    
    delegate.judgeMonitor = NO;
    [self.navigationController popViewControllerAnimated:YES];
}


@end
